package com.example.server.model;

import com.example.server.constant.ResultCodes;

public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> succeed(){
        return succeed(null);
    }

    public static <T> Result<T> succeed(T data){
        return getResult(ResultCodes.OK, data);
    }

    public static <T> Result<T> error(){
        return error(ResultCodes.ERROR);
    }

    public static <T> Result<T> error(ResultCodes resultCodes){
        return getResult(resultCodes, null);
    }

    public static <T> Result<T> error(String errorMessage){
        return error(400, errorMessage);
    }

    public static <T> Result<T> error(int code, String errorMessage){
        return new Result<>(code, errorMessage, null);
    }

    private static <T> Result<T> getResult(ResultCodes resultCodes, T data){
        return new Result<>(resultCodes.getCode(), resultCodes.getMessage(), data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
